<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->

    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #tableButtons>
          <slot name="tableButtons">
          <a-button
          v-if="options.add.show"
          v-auth="options.add.auth || []"
          v-role="options.add.role || []"
          @click="addAction(1)" type="primary"
          class="w-full lg:w-auto mt-2 lg:mt-0"
            >
              <template #icon>
                <icon-plus/>
              </template>
              {{ '新增客户' }}
            </a-button>
        </slot>
        <template v-for="button in buttonGroups">
          <a-button v-if="!button.children" type="primary" size="small" @click="button.action" :status="button.status?button.status:'default'">{{button.title}}</a-button>
          <a-dropdown trigger="hover" :popup-max-height="false" v-else>
            <a-button size="medium" type="primary" >
              {{button.title}}
              <template #icon>
                <icon-down/>
              </template>
            </a-button>
            <template #content>
              <a-doption v-for="buttonChild in button.children" @click="buttonChild.action">{{ buttonChild.title }}</a-doption>
            </template>
          </a-dropdown>
        </template>
      </template>
        <!-- 状态列 -->
        <template #status="{ record }">
          <a-switch
            :checked-value=1
            unchecked-value=2
            @change="changeStatus($event, record.id)"
            :default-checked="record.status == 1"
          />
        </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue' 
import financialAccount from '@/api/finance/financialAccount'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
const buttonGroups = [
  {title:'新增',action:'',children:[
    {title:'公司',action:()=>{addAction(2)}},
    {title:'供应商',action:()=>{addAction(3)}},
    {title:'员工',action:()=>{addAction(4)}}
    ]
  }
]

let typeDefault = 1
const crudRef = ref()

const addAction = (type) => {
  columns[1].addDefaultValue = type
  changeType(type)
  crudRef.value.addAction()
}

function changeType(type){
  if(type == 1){//客户
    columns[7].addDisplay = false
    columns[8].addDisplay = true
    columns[3].addDisplay = true
    columns[7].editDisplay = false
    columns[8].editDisplay = true
    columns[3].editDisplay = true
    columns[3].title = '客户'
    columns[3].dict.url = 'customers/customer/list'
    columns[3].dict.props.label = 'nick'
  }
  if(type == 2){//公司
    columns[7].addDisplay = true
    columns[8].addDisplay = false
    columns[3].addDisplay = false
    columns[7].editDisplay = true
    columns[8].editDisplay = false
    columns[3].editDisplay = false
    columns[3].dict.remote = ''
  }
  if(type == 3){//供应商
    columns[3].addDisplay = true
    columns[8].addDisplay = false
    columns[7].addDisplay = false
    columns[3].editDisplay = true
    columns[8].editDisplay = false
    columns[7].editDisplay = false
    columns[3].dict.url = 'logistics/supplier/list'
    columns[3].title = '供应商'
    columns[3].dict.props.label = 'name'
  }
  if(type == 4){//所属员工
    columns[3].addDisplay = true
    columns[8].addDisplay = false
    columns[7].addDisplay = false
    columns[3].editDisplay = true
    columns[8].editDisplay = false
    columns[7].editDisplay = false
    columns[3].dict.url = 'system/user/list'
    columns[3].dict.props.label = 'nickname'
    columns[3].title = '员工'
  }
}

const changeStatus = async (status, id) => {
    const response = await  financialAccount.changeStatus({ id, status })
    if (response.success) {
      Message.success(response.message)
    }
  }


const options = reactive({
  rowSelection: {
    showCheckedAll: true
  },
  requestParams: {
    orderBy : 'id',
    orderType : 'desc'
  },
  pk: 'id',
  pageLayout: 'fixed',
  beforeOpenEdit:function(record){
    changeType(record.type)
    return true;
  },
  tabs:{
    dataIndex:'type'
  },
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'drawer',
    width: "80%"
  },
  api: financialAccount.getList,
  recycleApi: financialAccount.getRecycleList,
  add: {
    show: true,
    api: financialAccount.save,
    auth: ['finance:financialAccount:save']
  },
  edit: {
    show: true,
    api: financialAccount.update,
    auth: ['finance:financialAccount:update']
  },
  delete: {
    show: true,
    api: financialAccount.deletes,
    auth: ['finance:financialAccount:delete'],
    realApi: financialAccount.realDeletes,
    realAuth: ['finance:financialAccount:realDeletes']
  },
  recovery: {
    show: true,
    api: financialAccount.recoverys,
    auth: ['finance:financialAccount:recovery']
  }
})

const columns = reactive([
  {
    title: "主键",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入主键"
    }
  },
  {
    title: "类型",
    dataIndex: "type",
    formType: "radio",
    addDisplay:false,
    editDisplay:false,
    search: true,
    addDefaultValue: typeDefault,
    commonRules: {
      required: true,
      message: "请输入类型"
    },
    dict: {
      name: "FinancialAccountType",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "账户名称",
    dataIndex: "name",
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入账户名称"
    }
  },
  {
    title: "",
    dataIndex: "user_id",
    formType: "select",
    hide:true,
    dict: {
      url: '',
      props: {label: 'username', value: 'id'},
    }
  },
  {
    title: "账户所属",
    addDisplay: false,
    editDisplay: false,
    dataIndex: "user"
  },
  {
    title: "币种",
    dataIndex: "currency",
    formType: "select",
    commonRules: {
      required: true,
      message: "请输入币种"
    },
    dict: {
      name: "currencyType",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "账户余额",
    dataIndex: "balance",
    formType: "input",
    addDisplay:false,
    editDisplay:false,
  },
  {
    title: "用户可见",
    dataIndex: "user_visible",
    formType: "switch",
    addDisplay: false,
    editDisplay: false,
    addDefaultValue: 0,
    checkedValue: 1,
    uncheckedValue: 0,
    hide: true,
  },
  
  {
    title: "账户状态",
    dataIndex: "status",
    formType: "switch",
    addDefaultValue: 1,
    dict: { name: 'data_status', props: { label: 'title', value: 'key' } },
    search: true,
    commonRules: {
      required: true,
      message: "请输入账户状态"
    },
    checkedValue: 1,
    uncheckedValue: 2
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "input",
    addDisplay: true,
    editDisplay: true,
    hide: false
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: false,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: false,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  }
])
</script>
<script> export default { name: 'finance:financialAccount' } </script>